package com.dayaokeji.rhythmschoolstudent.utils;

import com.dayaokeji.coursetable.bean.CourseModel;
import com.dayaokeji.rhythmschoolstudent.client.home.course.bean.SemesterBean;
import com.dayaokeji.server_api.domain.Course;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Gorden on 2017/11/1.
 */

public class CourseUtils {

    private static final String[] SEMSTER_ARRAY = {"2016-2017学年第二学期","2017-2018学年第一学期","2017-2018学年第二学期","2018-2019学年第一学期","2018-2019学年第二学期",
            "2019-2020学年第一学期","2019-2020学年第二学期","2020-2021学年第一学期","2020-2021学年第二学期"};

    /**
     * 解析课程数据，格式化成课表数据
     * @param courses
     * @return
     */
    public static List<CourseModel>[] paserCourseData(List<Course> courses,List<String> weeks) {
        List<CourseModel>[] courseModels = new ArrayList[7];
        for (int i = 0; i < courseModels.length; i++) {
            courseModels[i] = new ArrayList<>();
        }
        for (int i = 0; i < weeks.size(); i++) {
            List<CourseModel> courseModelList = new ArrayList<>();
            for (Course cours : courses) {
                if (cours.getActStartTime().contains(weeks.get(i))) {
                    CourseModel courseModel = new CourseModel();
                    courseModel.setCourseName(cours.getName());
                    courseModel.setId(cours.getCourseDetailId());
                    courseModel.setClassRoom(cours.getRoomName());
                    courseModel.setSection(cours.getStartTh());
                    courseModel.setSectionSpan(cours.getEndTh()-cours.getStartTh()+1);
                    courseModel.setWeek(cours.getWeekDay());
                    courseModel.setCourseFlag(cours.getColorFlag());
                    courseModelList.add(courseModel);
                    courseModels[i].addAll(courseModelList);
                }
            }
        }

        return courseModels;
    }

    /**
     * 学期
     * @return
     */
    public static List<SemesterBean> getSemesters() {
        final List<SemesterBean> semesterBeans = new ArrayList<>();
        for (int i = 0; i < SEMSTER_ARRAY.length; i++) {
            SemesterBean semesterBean = new SemesterBean();
            semesterBean.setSemester(i);
            semesterBean.setSemesterName(SEMSTER_ARRAY[i]);
            semesterBeans.add(semesterBean);
        }
        return semesterBeans;
    }

    public static SemesterBean getCurrentTerm() {
        List<SemesterBean> semesters = getSemesters();
        Logger.d(calermStr());
        for (SemesterBean semester : semesters) {
            if (semester.getSemesterName().equals(calermStr())) {
                return semester;
            }
        }
        return null;
    }

    public static String[] calCurrentTermDate() {
        Calendar instance = Calendar.getInstance();
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH) + 1;
        int afterYear = year+1;
        if (month < 8) { // 第一学期
            return new String[]{year+"-02-01 00:00:00",year+"-08-1 23:59:59"};
        }else{ // 第二学期
            return new String[]{year+"-08-01 00:00:00",afterYear+"-02-1 23:59:59"};
        }
    }


    private static String calermStr() {
        Calendar instance = Calendar.getInstance();
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH) + 1;
        int beforeYear = year -1 ;
        if (month < 8) { // 第一学期
            return beforeYear+"-"+year+"学年第一学期";
        }else{ // 第二学期
            return beforeYear+"-"+year+"学年第二学期";
        }
    }

}
